package file.interceptor;

import file.config.MyConfigProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;
import java.util.List;

/**
 * @author hh
 * @since 2024/12/7
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    private final LoginInterceptor loginInterceptor;
    private final IpInterceptor ipInterceptor;
    private final MyConfigProperties myConfigProperties;


    public WebMvcConfig(LoginInterceptor loginInterceptor, IpInterceptor ipInterceptor, MyConfigProperties myConfigProperties) {
        this.loginInterceptor = loginInterceptor;
        this.ipInterceptor = ipInterceptor;
        this.myConfigProperties = myConfigProperties;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if (myConfigProperties.isEnableLogin()) {
            List<String> excludePatterns = Arrays.asList("/login", "/doLogin", "/console", "/static/**", "/css/**", "/js/**", "/favicon.ico");
            registry.addInterceptor(loginInterceptor)
                    // 拦截哪些Ï请求
                    .addPathPatterns("/**")
                    // 不拦截哪些请求
                    .excludePathPatterns(excludePatterns);
        }
        registry.addInterceptor(ipInterceptor);
    }
}
